import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class PreferenceManager {
	
	/****************************************
	 * Attributes
	 ****************************************/
	private File preferenceFile;
	public String[] defaultMessages = {"", "", "", ""};
	
	
	/****************************************
	 * Constructor
	 ****************************************/
	public PreferenceManager(String preferenceFile) {
		this.preferenceFile = new File(preferenceFile);
		defaultMessages = readFile();
	}
	
	
	public void saveDefaultMessage() {	
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(preferenceFile));
			String outMsg = "";

			for (int i = 0; i < 4; i++) {
				outMsg += defaultMessages[i] + "\n";
			}
			
			out.write(outMsg);
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public String getDefaultMessage(int index) {
		if (index <= 4 && index > 0) {
			return readFile()[index];
		}
		
		else { 
			return null;
		}
	}
	
	
	public String[] getAllDefaultMessages() {
		return defaultMessages;
	}
	
	
	// Methods
	private String[] readFile() {
	        File file = preferenceFile;
	        BufferedReader reader = null;
	        
	        int count = 0;
	        String[] output = new String[4];

			try {
				reader = new BufferedReader(new FileReader(file));
				String text = null;

				// Repeat until all lines are read
				while ((text = reader.readLine()) != null) {
					// 
					output[count++] = text.toString();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (reader != null) {
						// Close the reader
						reader.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	        
	        return output;
		}

}
